#include <algorithm>
#include <cmath>
#include <cstdio>
#define lowbit(x) (x & -x)
#define id(i, j) ((i - 1) * m + j)

typedef long long ll;
const int N = 1e5 + 5, RAN = 1e4;
const ll inf = 0x3f3f3f3f3f3f3f3f;

int n, m, cnt;
int rt[N], val[N], buff[N];
ll f[N];

struct Line {
  ll k, b;
  Line() {}
  Line(const ll &k, const ll &b) : k(k), b(b) {}
  ll operator()(int x) const { return k * x + b; }
};

struct Node {
  int ls, rs;
  Line l;
} tr[N << 5];

void modify(int &p, int l, int r, const Line &a) {
  if (!p) { return p = ++cnt, tr[p].l = a, void(); }
  ll nl = a(l), nR = a(r), ol = tr[p].l(l), oR = tr[p].l(r);
  if (nl >= ol && nR >= oR) {
    return tr[p].l = a, void();
  } else if (nl <= ol && nR <= oR)
    return;
  int mid = floor((l + r) / 2.0);
  ll nM = a(mid), oM = tr[p].l(mid);
  if (nM >= oM) {
    Line tmp = tr[p].l;
    tr[p].l = a;
    if (nl < ol) modify(tr[p].ls, l, mid, tmp);
    else
      modify(tr[p].rs, mid + 1, r, tmp);
  } else {
    if (nl > ol) modify(tr[p].ls, l, mid, a);
    else
      modify(tr[p].rs, mid + 1, r, a);
  }
}

ll query(int p, int l, int r, int x) {
  if (!p) return -inf;
  ll v = tr[p].l(x);
  if (l == r) return v;
  int mid = floor((l + r) / 2.0);
  if (x <= mid) return std::max(v, query(tr[p].ls, l, mid, x));
  else
    return std::max(v, query(tr[p].rs, mid + 1, r, x));
}

void Modify(int p, const Line &a) {
  while (p) { modify(rt[p], -RAN, RAN, a), p -= lowbit(p); }
}

ll Query(int p, int x) {
  ll ans = -inf;
  while (p <= m)
    ans = std::max(ans, query(rt[p], -RAN, RAN, x)), p += lowbit(p);
  return ans;
}

inline void solve() {
  for (int i = n; i >= 1; --i) {
    for (int j = m; j >= 1; --j) {
      f[id(i, j)] = Query(j, buff[id(i, j)]) + val[id(i, j)] -
                    1ll * buff[id(i, j)] * (i + j);
      if (i == n && j == m) f[id(i, j)] = 0;
      Modify(j, Line(i + j, f[id(i, j)]));
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n * m; ++i) scanf("%d", &buff[i]);
  for (int i = 1; i <= n * m; ++i) scanf("%d", &val[i]);
  solve();
  printf("%lld\n", f[id(1, 1)]);
  return 0;
}